package le.grand;

import java.io.PrintStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import de.schlichtherle.io.File;
import de.schlichtherle.io.FileReader;
import dnl.utils.text.table.TextTable;
import dnl.utils.text.table.TextTreeTable;

public class OrderInTheHouse {

	Map<String, FlightInfo> flights = new HashMap<String, FlightInfo>();

	public void addFile(File f) throws Exception {
		FileParser fp = new FileParser();
		FlightInfo flightInfo = fp.parse(new FileReader(f));

		FlightInfo existingFlightInfo = this.flights.get(flightInfo.getFlightName());
		if (existingFlightInfo == null) {
			this.flights.put(flightInfo.getFlightName(), flightInfo);
			System.out.println(">>> New FlightInfo: " + flightInfo);
		} else {
			System.out.println(">>> Existing FlightInfo: " + flightInfo);
			existingFlightInfo.append(flightInfo);
		}
	}

	public Collection<String> getFlightNames() {
		return flights.keySet();
	}

	public void print(PrintStream ps) {
		Collection<FlightInfo> fs = flights.values();
		for (FlightInfo fi : fs) {
			ps.println("=======================================================");
			printFlight2(fi, ps, 40);
		}
	}

	public void printFlight(String flightName, PrintStream ps, int nameColumnSize) {
		FlightInfo flightInfo = flights.get(flightName);
		printFlight2(flightInfo, ps, nameColumnSize);
	}

	void printFlight(FlightInfo flightInfo, PrintStream ps, int nameColumnSize) {
		ps.print("Flight: " + flightInfo.getFlightName());
		ps.print("  Date: " + flightInfo.getDay());
		ps.print("  Departure: " + flightInfo.getDeparture());
		ps.println("  Arrival: " + flightInfo.getArrival());
		ps.println("\n");
		TextTable tt = new TextTable(new FlightInfoTableModel(flightInfo, nameColumnSize));
		tt.printTable(ps, 0);
		ps.println("\n");
		ps.println("Total passengers: " + flightInfo.getTotalNumOfPassengers());
		ps.println("Total children: " + flightInfo.getTotalNumOfChildren());
	}

	void printFlight2(FlightInfo flightInfo, PrintStream ps, int nameColumnSize) {
		ps.print("Flight: " + flightInfo.getFlightName());
		ps.print("  Date: " + flightInfo.getDay());
		ps.print("  Departure: " + flightInfo.getDeparture());
		ps.println("  Arrival: " + flightInfo.getArrival());
		ps.println("\n");
		TextTreeTable tt = new TextTreeTable(new FlightInfoTableModel(flightInfo, nameColumnSize));
		tt.setHierarchicalColumn(0);
		tt.printTable(ps, 0);
		ps.println("\n");
		ps.println("Total passengers: " + flightInfo.getTotalNumOfPassengers());
		ps.println("Total children: " + flightInfo.getTotalNumOfChildren());
	}

	public void processArchive(File arc) throws Exception {
		java.io.File[] files = arc.listFiles();

		for (java.io.File file : files) {
			File f = (File) file;
			System.out.println("++++++++++++++++++++++++++++++++++++++++\n" + "Reading file: "
					+ f.getName());
			this.addFile(f);
		}
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// File f = new
		// File("/home/daniel/Downloads/19 Arrival BBG 752 Aquila.txt");
		File arc = new File(
				"/home/daniel/Downloads/fwdfwemailing19arrivalbbg752leg19arrivalbbg75.zip");
		OrderInTheHouse ord = new OrderInTheHouse();
		ord.processArchive(arc);
		ord.print(System.out);
	}

}
